﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Room
{
    class BlockLists
    {
        private const int cbl = 50;
        public const int ccbl = 5;
        static public string[,] blockIPs = new string[cbl, ccbl]; //BlockIPs
        static public string[,] blockNames = new string[cbl, ccbl]; //BlockNames

        //[ip-adress/name] [banby] [baned] [unbaned] [reason]

        static public void bAutoUnBan(int tp)
        {
            int count = 0;
            switch (tp)
            {
                case 0:
                    {
                        for (int i = 0; i < ccbl; ++i)
                        {
                            if (blockIPs[i, 2] != blockIPs[i, 3])
                            {
                                DateTime d = DateTime.Now;
                                DateTime db = new DateTime();
                                db = DateTime.Parse(blockIPs[i, 3]);
                                int t = DateTime.Compare(d, db);
                                if (t != -1)
                                {
                                    if (bUnBan(0, blockIPs[i, 0])) count++;
                                    Core.write(3, "[bAutoUnBan] Auto unblock for \"" + blockIPs[i, 0] + "\".");//debug
                                }
                            }
                        }
                        Core.write(3, "[bAutoUnBan] Automatically unblocked " + count.ToString() + " addresses."); //debug
                    } break;
                case 1:
                    {
                        for (int i = 0; i < ccbl; ++i)
                        {
                            if (blockNames[i, 2] != blockNames[i, 3])
                            {
                                DateTime d = DateTime.Now;
                                DateTime db = new DateTime();
                                db = DateTime.Parse(blockNames[i, 3]);
                                int t = DateTime.Compare(d, db);
                                if (t != -1)
                                {
                                    if (bUnBan(1, blockNames[i, 0])) count++;
                                    Core.write(3, "[bAutoUnBan] Auto unblock for \"" + blockIPs[i, 0] + "\".");//debug
                                }
                            }
                        }
                        Core.write(3, "[bAutoUnBan] Automatically unblocked " + count.ToString() + " names.");
                    } break;
            }
        }

        static public void bSetNullTable(int tp)
        {
            switch (tp)
            {
                case 0:
                    {
                        for (int i = 0; i < cbl; ++i)
                            for (int j = 0; j < ccbl; ++j)
                                blockIPs[i, j] = "(none)";
                        Core.write(3, "[bSetNullTable] All elements in BlockIPs set Null."); //debug
                    } break;
                case 1:
                    {
                        for (int i = 0; i < cbl; ++i)
                            for (int j = 0; j < ccbl; ++j)
                                blockNames[i, j] = "(none)";
                        Core.write(3, "[bSetNullTable] All elements in BlockNames set Null."); //debug
                    } break;
            }
        }

        static public void bWriteTable(int tp)
        {
            switch (tp)
            {
                case 0:
                    {
                        Core.write(0, "Забаненные IP-адресса:\n" +
                        "[ip-adress] [banby] [baned] [unbaned] [reason]"); //info
                        for (int i = 0; i < cbl; ++i)
                        {
                            for (int j = 0; j < ccbl; ++j) if (blockIPs[i, 0] != "(none)") Console.Write("[" + blockIPs[i, j] + "] "); else break; //info
                            if (blockIPs[i, 0] != "(none)") Console.WriteLine(); else continue;
                        }
                    } break;
                case 1:
                    {
                        Core.write(0, "Забаненные пользователи:\n" +
                        "[name] [banby] [baned] [unbaned] [reason]"); //info
                        for (int i = 0; i < cbl; ++i)
                        {
                            for (int j = 0; j < ccbl; ++j) if (blockNames[i, 0] != "(none)") Console.Write("[" + blockNames[i, j] + "] "); else break; //info
                            if (blockNames[i, 0] != "(none)") Console.WriteLine(); else continue;
                        }
                    } break;
            }
        }

        static public int bIsRangeMas(int tp)
        {
            int ind = -1;
            switch (tp)
            {
                case 0:
                    {
                        for (int i = 0; i < cbl; ++i)
                            if (blockIPs[i, 0] == "(none)")
                            {
                                ind = i;
                                break;
                            }
                    } break;
                case 1:
                    {
                        for (int i = 0; i < cbl; ++i)
                            if (blockNames[i, 0] == "(none)")
                            {
                                ind = i;
                                break;
                            }
                    } break;
            }
            return ind;
        }

        static public bool bAddBan(int tp, string ip, string bb, string ch, string rea)
        {
            switch (tp)
            {
                case 0:
                    {
                        if (bIsRangeMas(0) == -1)
                        {
                            Core.write(3, "[bAddBan] In BlockList IP not null rows. Not Baned"); //error
                            return false;
                        }
                        else
                        {
                            try
                            {
                                if (bBaned(0, ip) == -1)
                                {
                                    int ind = bIsRangeMas(0);
                                    string v0 = "", v1 = "", v2 = "", v3 = "", v4 = "";
                                    v0 = ip;
                                    v1 = bb;
                                    DateTime d = new DateTime();
                                    d = DateTime.Now;
                                    v2 = d.ToString();
                                    d = d.AddHours(Convert.ToInt32(ch));
                                    v3 = d.ToString();
                                    v4 = rea;
                                    blockIPs[ind, 0] = v0;
                                    blockIPs[ind, 1] = v1;
                                    blockIPs[ind, 2] = v2;
                                    blockIPs[ind, 3] = v3;
                                    blockIPs[ind, 4] = v4;
                                    Core.write(3, "[bAddBan] IP \"" + v0 + "\" baned."); //debug
                                    return true;
                                }
                                else
                                {
                                    Core.write(3, "[bAddBan] The address is already blocked."); //debug
                                    return false;
                                }
                            }
                            catch (FormatException)
                            {
                                Core.write(1, "[bAddBan] FormatException. Not baned."); // error
                                return false;
                            }
                        }
                    } break;
                case 1:
                    {
                        if (bIsRangeMas(1) == -1)
                        {
                            Core.write(3, "[bAddBan] In BlockList Name not null rows. Not Baned"); //error
                            return false;
                        }
                        else
                        {
                            try
                            {
                                if (bBaned(1, ip) == -1)
                                {
                                    int ind = bIsRangeMas(1);
                                    string v0 = "", v1 = "", v2 = "", v3 = "", v4 = "";
                                    v0 = ip;
                                    v1 = bb;
                                    DateTime d = new DateTime();
                                    d = DateTime.Now;
                                    v2 = d.ToString();
                                    d = d.AddHours(Convert.ToInt32(ch));
                                    v3 = d.ToString();
                                    v4 = rea;
                                    blockNames[ind, 0] = v0;
                                    blockNames[ind, 1] = v1;
                                    blockNames[ind, 2] = v2;
                                    blockNames[ind, 3] = v3;
                                    blockNames[ind, 4] = v4;
                                    Core.write(3, "[bAddBan] User \"" + v0 + "\" baned."); //debug
                                    return true;
                                }
                                else
                                {
                                    Core.write(3, "[bAddBan] The name is already blocked."); //debug
                                    return false;
                                }
                            }
                            catch (FormatException)
                            {
                                Core.write(1, "[bAddBan] FormatException. Not baned."); // error
                                return false;
                            }
                        }
                    } break;
                default: return false; break;
            }
        }

        static public int bBaned(int tp, string value)
        {
            int ind = -1;
            switch (tp)
            {
                case 0:
                    {
                        for (int i = 0; i < cbl; ++i)
                            if (blockIPs[i, 0] == value)
                            {
                                ind = i;
                                break;
                            }
                        return ind;
                    } break;
                case 1:
                    {
                        for (int i = 0; i < cbl; ++i)
                            if (blockNames[i, 0] == value)
                            {
                                ind = i;
                                break;
                            }
                        return ind;
                    } break;
                default: return ind;
            }
        }

        static public bool bUnBan(int tp, string value)
        {
            int ind = bBaned(tp, value);
            switch (tp)
            {
                case 0:
                    {
                        if (ind != -1)
                        {
                            for (int i = 0; i < ccbl; ++i) blockIPs[ind, i] = "(none)";
                            if (bBaned(0, value) == -1)
                            {
                                Core.write(3, "[bUnBan] The address \"" + value + "\" unblocked.");
                                return true;
                            }
                            else return false;
                        }
                        else
                        {
                            Core.write(3, "[bUnBan] The address \""+value+"\" is not blocked.");//Debug
                            return false;
                        }
                    } break;
                case 1:
                    {
                        if (ind != -1)
                        {
                            for (int i = 0; i < ccbl; ++i) blockNames[ind, i] = "(none)";
                            if (bBaned(1, value) == -1)
                            {
                                Core.write(3, "[bUnBan] The name \"" + value + "\" unblocked.");
                                return true;
                            }
                            else return false;
                        }
                        else
                        {
                            Core.write(3, "[bUnBan] The name \"" + value + "\" is not blocked.");//Debug
                            return false;
                        }
                    } break;
                default: return false;
            }
        }
    }
}
